package com.yunhe.option.service.data.impl;

import com.yunhe.option.domain.data.Weather;
import com.yunhe.common.model.PageParam;
import com.yunhe.option.repository.data.WeatherRepository;
import com.yunhe.option.service.data.WeatherService;
import com.yunhe.common.util.QueryUtil;
import com.yunhe.common.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 天气Service
 * @desc 天气服务类实现
 * @author liuronglei
 */
@Service
public class WeatherServiceImpl implements WeatherService {
    @Autowired
    private WeatherRepository weatherRepository;

    /**
     * 新增天气对象
     * @param weather 天气对象
     */
    @Override
    @Transactional
    public Weather createWeather(Weather weather) {
        Weather newWeather = weatherRepository.save(weather);
        return newWeather;
    }

    /**
     * 批量新增天气对象
     * @param weatherList 天气对象列表
     */
    @Override
    @Transactional
    public Iterable<Weather> createWeather(List<Weather> weatherList) {
        Iterable<Weather> newWeathers = weatherRepository.saveAll(weatherList);
        return newWeathers;
    }

    /**
     * 根据条件获得天气对象列表
     * @param areaCode 地区代码
     * @param dtime 时间
     * @param pageParam 分页对象
     */
    @Override
    public Iterable<Weather> findWeathers(Long areaCode, List<Date> dtime, PageParam pageParam) {
        Specification spec = (Specification<Weather>) (root, query, cb) -> {
            List<Predicate> criteria = new ArrayList<>();
            if (areaCode != null) {
                Path<String> areaCodePath = root.get("areaCode");
                criteria.add(cb.equal(areaCodePath, areaCode));
            }
            if (CollectionUtil.isNotEmpty(dtime)) {
                Path<Date> dtimePath = root.get("dtime");
                List<Date> dtimeList = dtime;
                if (dtimeList.size() == 1) {
                    criteria.add(cb.equal(dtimePath, dtimeList.get(0)));
                } else if (dtimeList.size() == 2) {
                    criteria.add(cb.between(dtimePath, dtimeList.get(0), dtimeList.get(1)));
                }
            }
            if (criteria.size() == 1) {
                query.where(criteria.get(0));
            } else {
                query.where(criteria.toArray(new Predicate[0]));
            }
            return null;
        };
        if (QueryUtil.needPaging(pageParam)) {
            return weatherRepository.findAll(spec, QueryUtil.getPageRequest(pageParam));
        } else {
            Sort sort = pageParam == null ? null : QueryUtil.getSort(pageParam.getSort());
            if (sort != null) {
                return weatherRepository.findAll(spec, sort);
            } else {
                return weatherRepository.findAll(spec);
            }
        }

    }

}
